/**
 * Description:一些注释<br/>
 * CreateTime:2019-03-26<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
package cn.cnsy123.bookwebsiteserver.controller.kaptcha;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;

import cn.cnsy123.bookwebsiteserver.vo.layui.CommonResult;

/**
 * Description:一些注释<br/>
 * CreateTime:2019-03-26<br/>
 * Creater: yangpengfei360@126.com <br/>
 * 
 * @author yangpengfei
 */
@Controller
public class KaptchaController {
    
    private static final Logger logger = LoggerFactory.getLogger(KaptchaController.class);
    
    @Autowired
    private Producer captchaProducer;

    @RequestMapping("/kaptcha")
    public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        // 生成验证码
        String capText = captchaProducer.createText();
        logger.info("本次生成验证码:{}",capText);
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
        // 向客户端写出
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }
    
    @PostMapping(value = "/imgCode/validate")
    @ResponseBody
    public CommonResult<Boolean> validateImgCode(String imgCode,HttpServletRequest request){
        CommonResult<Boolean> result= null;
        try {
            result = new CommonResult<Boolean>(1,"验证码输入错误",Boolean.valueOf("false"));
            String attribute = (String)request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
            if(attribute.equalsIgnoreCase(imgCode)) {
                result.setCode(0);
                result.setData(Boolean.valueOf("true"));
            }
        } catch (Exception e) {
            result = new CommonResult<Boolean>(1,"验证码校验失败",Boolean.valueOf("false"));
        }
        return result;
    }
}
